
from playwright.sync_api import sync_playwright
from lxml import etree
import csv

def clean_text(text):
    """清理文本中的特殊字符，将\xa0替换为空格"""
    if text is None:
        return ""
    return text.replace('\xa0', ' ').strip()

with sync_playwright() as p:
    # 1. 启动浏览器（headless=True表示无界面运行）
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    
    # 2. 访问目标动态网站，等待页面加载完成
    url = 'https://vip.titan007.com/count/goalCount.aspx?t=2&sid=2702239&cid=1&l=0'
    page.goto(url, wait_until="networkidle")  # 等待网络空闲（确保JS执行完）
    
    # 3. 获取渲染后的HTML代码
    html = page.content()
    # print(html)

    e = etree.HTML(html)
    
    # 提取赛事名称（排除表头和空行）
    match_name_elements = e.xpath('//span/table//tr[position()>1 and position()<last()]/td[1]/text()')
    # 去掉空字符，并且去掉最后一行
    match_name_elements = [clean_text(name) for name in match_name_elements]

    # 时间
    time_num_elements = e.xpath('//span/table//tr[position()>1]/td[2]')
    time_num_elements = [clean_text(time.text) for time in time_num_elements]

    # 初盘
    td_elements = e.xpath('//span/table//tr[position()>1]/td[3]')
    rank_num_elements = []
    for td in td_elements:
        # 获取该td下所有文本内容并拼接
        text_content = ''.join(td.xpath('.//text()')).strip()
        rank_num_elements.append(clean_text(text_content))
    print(rank_num_elements)

    # 主队
    zhudui_num_elements = e.xpath('//span/table//tr[position()>1]/td[4]')
    zhudui_num_elements = [clean_text(zhudui.text) for zhudui in zhudui_num_elements]

    # 比分
    td_elements_5 = e.xpath('//span/table//tr[position()>1]/td[5]')
    bifen_num_elements = []
    for td in td_elements_5:
        # 获取该td下所有文本内容并拼接
        text_content = ''.join(td.xpath('.//text()')).strip()
        bifen_num_elements.append(clean_text(text_content))
    # print(bifen_num_elements)

    # 客队
    kedui_num_elements = e.xpath('//span/table//tr[position()>1]/td[6]')
    kedui_num_elements = [clean_text(kedui.text) for kedui in kedui_num_elements]

    # 写入CSV文件
    with open('数据.csv', 'w', newline='', encoding='utf-8-sig') as csvfile:
        writer = csv.writer(csvfile)
        # 写入表头
        writer.writerow(['赛事', '时间', '终盘','主队','比分','客队'])

        # 写入数据行
        for name, time, zhonpan, zhudui, bifen, kedui in zip(
            match_name_elements, time_num_elements, rank_num_elements,
            zhudui_num_elements, bifen_num_elements, kedui_num_elements):
            writer.writerow([name, time, zhonpan, zhudui, bifen, kedui])

    # 4. 关闭浏览器
    browser.close()